
package geomatic.android;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.*;
import org.geonames.*;

public class MainActivity extends Activity 
{
	private WikipediaArticleListView articlesView;
	private LinearLayout layout;
	private Button submit;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        
        layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        
        articlesView = new WikipediaArticleListView(this);
		articlesView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        final EditText latitude = new EditText(this);
        final EditText longitude = new EditText(this);

        
        // PGE Park in Portland Oregon:   45.521694,-122.691806

        latitude.setText("45.521694");
        longitude.setText("-122.691806");
        
        submit = new Button(this);
        submit.setText("find nearby Wikipedia articles");
        submit.setOnClickListener(new OnClickListener()
        {
			public void onClick(View v)
			{
				articlesView.clear();
				
				submit.setEnabled(false);
				
				double lat = Double.parseDouble(latitude.getText().toString());
				double lon = Double.parseDouble(longitude.getText().toString());
			
				fetchArticlesAsync(lat, lon, "en", 5);
				
			}
        	
        });
        
        layout.addView(latitude);
        layout.addView(longitude);
        
        layout.addView(submit);
        
        layout.addView(articlesView);
        
        setContentView(layout);
    }
	
	
	protected void fetchArticlesAsync(final double lat, final double lon, final String language, final int radius)
	{
		
		Runnable fetch = new Runnable()
		{
			public void run()
			{
				
				try
				{
					final List<WikipediaArticle> articles = WebService.findNearbyWikipedia(lat, lon, language, radius /* kilometers */);
					
					System.out.println("found " + articles.size() + " Wikipedia articles");
			
					Runnable updateUI;
					if (articles.size() > 0)
					{
						updateUI = new Runnable()
						{
							public void run()
							{
								articlesView.setWikipediaArticles(articles);
							}
						};
					}
					else
					{
						updateUI = new Runnable()
						{
							public void run()
							{
								articlesView.clear();
								TextView text = new TextView(MainActivity.this);
								text.setText("No articles found");
								articlesView.setEmptyView(text);
							}
						};
					}
					MainActivity.this.runOnUiThread(updateUI);
				}
				catch (Exception ex)
				{
					ex.printStackTrace();
				}
				finally
				{
					Runnable updateUI = new Runnable()
					{
						public void run()
						{
							submit.setEnabled(true);
						}
					};
					MainActivity.this.runOnUiThread(updateUI);
				}
			}
		};
		
		startThread(fetch);
		
	}

    protected void openWebBrowser(String url)
    {
    	Log.i(this.getClass().getName(), "about to launch browser, url: " + url);
    	Intent i = new Intent(Intent.ACTION_VIEW);
    	i.setData(Uri.parse(url));
    	startActivity(i);
    }
    
    protected void startThread(String name, Runnable r)
    {
    	Thread t = new Thread(r);
    	t.setName(name);
    	t.start();
    }
    
    protected void startThread(Runnable r)
    {
    	String name = this.getClass().getName() + " thread";
    	startThread(name, r);
    }
	
}